<?php
class Bewerten {
	
	private $type;
	private $dbhandler;
	private $apphelper;
	
	function Bewerten($type) {
		$this -> type = $type;
		$this -> dbhandler = new DBhandler();
	}
	
	public function ladeBewertung($id) {
		$this -> apphelper = new AppHelper();
		$type = $this -> apphelper -> typeURL($this -> type);
		$result = array();
		$result_kriterien = array();
		$return = array();
		$select = ("select * FROM punkte WHERE art_id = '".$id."'");
		$result = $this -> dbhandler -> fetch_all_array($select);
		if (!empty($result)) {
			for ($a=0;$a<count($result);$a++) {
				$return[$a][0] = $result[$a]["id"];	
				$return[$a][1] = $this -> dbhandler -> fetch_first_string("select beschreibung from kriterium WHERE id = '".$result[$a]["kriterium_id"]."'",'beschreibung');
				$return[$a][2] = $this -> dbhandler -> fetch_first_string("select beschreibung from art WHERE id = '".$result[$a]["art_id"]."'",'beschreibung');	
				$return[$a][3] = $this -> buildSelectbox($result[$a]["id"], $result[$a]["punkte"]);
				$return[$a][4] = '<a href="#"  onClick="window.open(\''.$type.'?addCommentPunkte&id='.$result[$a]["id"].'\',\'\',\'scrollbars=yes,width=540,height=240\');return false;" style="text-decoration:none;color:#000000"><img src="public/images/icons/pencil.png"></a>';	
				$return[$a][5] = $this -> dbhandler -> fetch_first_string("select beschreibung from kategorie WHERE id = '".$result[$a]["kategorie_id"]."'",'beschreibung');	
			}
		}
		return json_encode($return);
	}
	
	
	public function buildSelectbox($id, $punkte) {
		$string = '<select id="'.$id.'" onChange="changeBewertungAjax('.$id.')">';
		for ($a=0;$a<=10;$a++) {
			if ($a == $punkte) {
				$string .= '<option value="'.$a.'" selected>'.$a.'</option>';
			}
			else {
				$string .= '<option value="'.$a.'">'.$a.'</option>';
			}		
		}
		$string .= '</select>';
		return $string;
	}
	
	public function speicherPunkte($id, $value) {
		$update = ("update punkte set punkte = '".$value."' WHERE id = '".$id."'");
		$this -> dbhandler -> update_row($update);		
	}

	public function ladeKommentarPunkte($id) {
		$select = ("select id, kommentar FROM punkte WHERE id = '".$id."'");
		$result = $this -> dbhandler -> fetch_first_row($select);
		return $result;
	}
	
	public function speicherKommentarPunkte($id, $value) {
		if (trim($value) != '') {
			$update = ("update punkte set kommentar = '".$value."' WHERE id = '".$id."'");
			$this -> dbhandler -> update_row ($update);
		}
	}
}














?>